package com.qitianyong.adapter;

import java.util.List;

import com.qitianyong.selfclass.ItemInformation;
import com.qitianyong.sgkusbcamera.R;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class GKAdapter extends android.widget.BaseAdapter {

    private LayoutInflater __inflater;
    private List<? extends Object> __list;

    public GKAdapter(Activity activity, List<? extends Object> list){

        __inflater = activity.getLayoutInflater();
        __list = list;

        Log.i("listitem", "adapter:"+__list.size());
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if(__list == null) return 0;

        return __list.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return __list.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        Object object = __list.get(arg0);
        if(object instanceof ItemInformation){

            arg1 = getCofigItemInfomationView(arg1, arg0);
        }

        return arg1;
    }

    private View getCofigItemInfomationView(View view, int index){

        TIIHolder tiiHolder = null;
        if(view == null){

            tiiHolder = new TIIHolder();
            view = __inflater.inflate(R.layout.config_item, null);

            tiiHolder.__tv_item = (TextView)view.findViewById(R.id.tv_content);
            tiiHolder.__iv_status = (ImageView)view.findViewById(R.id.iv_select);

            view.setTag(tiiHolder);
        }else{

            tiiHolder = (TIIHolder)view.getTag();
        }

        ItemInformation itemInfo = (ItemInformation)__list.get(index);
        tiiHolder.__tv_item.setText(itemInfo.__content);

        if(itemInfo.__status){
            tiiHolder.__iv_status.setImageResource(R.drawable.pic_chioce);
        }else{
            tiiHolder.__iv_status.setImageBitmap(null);
        }

        return view;
    }
}
